<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ToolAction\ReturnActions;
use App\Admin\Actions\Post\Restore;
use App\Admin\Extensions\Excel\MedicalExporter;
use App\Admin\Repositories\Medical;
use App\Models\Medical as ModelsMedical;
use App\Models\MemberDoctor;
use App\Models\User;
use App\Support\Data;
use Carbon\Carbon;
use Dcat\Admin\Grid\Tools;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class MedicalController extends AdminController
{
    /**
     * Make a grid builder.https://lc.resources.medcase.com.cn/default/20240417142600-661f6b7846170.jpg,https://lc.resources.medcase.com.cn/default/20240417142602-661f6b7ab8024.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Medical(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->addTableClass(['table-text-center']);
            // $grid->disableCreateButton();
            $grid->model()->with(['memberDoctor']);
            $user_id = request()->input('user_id');
            if (!empty($user_id) || isset($user_id)) {
                $grid->model()->where('user_id', $user_id);
            }

            if (!Admin::user()->isAdministrator()) {
                /**
                 * 工具按钮.
                 */
                $grid->tools(function (Tools $tools) {
                    $tools->append(new ReturnActions());
                });
            }




            $grid->export(new MedicalExporter());
            $grid->column('id')->sortable();
            $grid->column('name', '患者姓名');
            $grid->column('memberDoctor.real_name', '医生姓名');
            $grid->column('source', '来源')->display(function ($id) {
                $source = Data::getMedicalSource();
                return isset($source[$id]) ? $source[$id] : (isset($this->source_input) && !empty($this->source_input) ? $this->source_input : '未填写-未选择');
            })->label();
            if (Admin::user()->can('medical-audit')) {
                $grid->status()->using(['未审核', '已审核', '驳回'])->badge([
                    0 => 'primary',
                    1 => 'success',
                    2 => 'danger'
                ]);
            }
            $grid->agreement_status('协议下载状态')->using(['未下载', '已下载'])->badge([
                1 => 'success',
                0 => 'danger'
            ]);
            $grid->column('agreement_file', '协议文件')->display(function ($url) {
                if ($this->status == 1) {
                    if (empty($url)) {
                        return '<a href="/admin/download?cmd=agreement' . '&medical_id=' . $this->id . '" class="text-custom" style="margin-right: 10px;"><i class="fa fa-download"></i>生成协议</a>';
                    } else {
                        return '<a href="/admin/download?cmd=agreement' . '&medical_id=' . $this->id . '" class="text-custom" style="margin-right: 10px;"><i class="fa fa-download"></i>下载协议</a>';
                    }
                } else {
                    return '病例未审核';
                }
            });
            $grid->column('created_at');

            $grid->model()->orderBy('id', 'desc');


            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('medical-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('medical-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $grid->disableDeleteButton();
            }

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name', '患者姓名');
                $filter->equal('source', '来源')->select([1 => '消化科', 2 => '内科', 3 => '心理科', 4 => '其他']);
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '状态', [0 => '未审核', 1 => '已审核', 2 => '驳回']);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    // @permissions
                    if (!Admin::user()->can('medical-delete')) {
                        $actions->disableDelete();
                    }
                    $actions->disableEdit();
                    // $actions->disableView();
                    $actions->append(new Restore(ModelsMedical::class));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Medical(), function (Show $show) {
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $show->disableDeleteButton();
            }

            // @permissions
            if (!Admin::user()->can('medical-update') || (Admin::user()->id != $show->model()->admin_id) && !Admin::user()->isAdministrator()) {
                $show->disableEditButton();
            }

            $show->field('id');
            // $show->field('title');
            // $show->field('act_id', '活动专题')->as(function ($id) {
            //     return Activity::find($id)->activity_name ?? '未关联共识';
            // })->label();
            // $show->field('medical_history')->unescape();
            // $show->field('symptoms')->unescape();
            // $show->field('diagnosis')->unescape();
            // $show->field('treatment')->unescape();
            // $show->field('detail')->unescape();

            // $show->field('cate_id')->as(function ($c_id) {
            //     return Category::find($c_id)->title ?? '';
            // })->label();
            $show->field('atm_coding', '编码');
            // $show->field('user_id', '用户名')->as(function ($user_id) {
            //     $member = Member::find($user_id);
            //     return $member->username . '  /  (' . $member->realname . ' )';
            // })->label();
            // $show->medical_data()->file();
            $show->invoice_img('发票图片')->image();
            $show->discharge_summary_img('出院小结图片')->image();
            // $show->field('created_at');
            // $show->field('updated_at');
            // $show->field('examine_time');
            // $show->field('reason');
            // $show->status()->using(['1' => '显示', '0' => '不显示']);
            // $show->is_del()->using(['1' => '已删除', '0' => '未删除']);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Medical(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableViewButton();
            // $form->disableSubmitButton();
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $form->disableDeleteButton();
            }





            $form->hidden('status')
                // ->customFormat(function ($v) {
                //     return $v == 1 ? 1 : 0;
                // })
                ->saving(function ($v) {
                    return $v;
                });
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;患者基本信息');
                // 设置字段宽度
                $form->width(9, 2);

                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('name', '患者姓名')->required();
                    $form->number('age', '年龄')->placeholder('请输入年龄')->required();

                    $form->radio('sex', '性别')->options([1 => '男', 2 => '女'])->required();
                    $form->text('height', '身高')->required()->help('CM');
                    $form->text('weight', '体重')->required()->help('KG');

                    $form->radio('source', '病例来源')->when(4, function (Form\BlockForm $form) {
                        $form->text('source_input', '其他来源')->placeholder('其他病例来源');
                    })->options(Data::getMedicalSource());
                    $form->text('bim', 'BIM')->placeholder('请输入内容')->required();
                    $form->radio('bim_radio', '判断指标')->options(Data::getBimType())->required();

                    $form->checkbox('symptom', '主诉症状')->options(Data::getSymptom())->when(11, function (Form\BlockForm $form) {
                        $form->text('symptom_input', '其他');
                    })->required()->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                    $form->radio('disease_code', '疾病状态')->options([1 => '新发', 2 => '已确诊疾病'])->required();
                    $form->text('course_disease', '病程')->required();

                    $form->radio('drink_alcohol', '饮酒史')->options([1 => '是', 2 => '否'])->when(1, function (Form\BlockForm $form) {
                        $form->text('drink_alcohol_quantity', '饮酒量')->placeholder('每日（）ml');
                        $form->text('drink_alcohol_frequency', '饮酒频率')->placeholder('每周（）次');
                    });

                    $form->radio('smoking_history', '吸烟史')->options([1 => '是', 2 => '否'])->when(1, function (Form\BlockForm $form) {
                        $form->text('smoking_history_year', '吸烟年限')->placeholder('吸烟（）年');
                        $form->text('smoking_history_quantity', '吸烟量')->placeholder('每日（）根');
                    });
                });

                $form->column(6, function (Form\BlockForm $form) {


                    $form->radio('family_historyL', '家族史')->options([1 => '是', 2 => '否']);
                    $form->radio('past_historyL', '既往史')->options([1 => '是', 2 => '否']);

                    $form->text('medical_history', '其他既往史');

                    $form->radio('allergy', '过敏史')->options([1 => '无', 2 => '有'])->when(2, function (Form\BlockForm $form) {
                        $form->text('allergy_input', '其他过敏史')->placeholder('请输入其他过敏史');
                    });

                    $form->radio('check', '既往检查')->options([1 => '是', 2 => '否'])
                        ->when(1, function (Form\BlockForm $form) {
                            $form->checkbox('check_true', '检查项目')
                                ->options(Data::getPastCheckSuccess())
                                ->when(2, function (Form\BlockForm $form) {
                                    $form->text('check_true_laboratory', '实验室检查结果');
                                })->when(3, function (Form\BlockForm $form) {
                                    $form->text('check_true_image', '影像学检查结果');
                                })->when(4, function (Form\BlockForm $form) {
                                    $form->text('check_true_pathology', '病理检查结果');
                                })->when(5, function (Form\BlockForm $form) {
                                    $form->text('check_true_other', '其他检查结果');
                                })->saving(function ($paths) {
                                    return implode(',', $paths);
                                });
                        })->when(2, function (Form\BlockForm $form) {
                            $form->checkbox('check_false', '未进行检查原因')->options(Data::getPastCheckFail())->when(6, function (Form\BlockForm $form) {
                                $form->text('check_false_input', '其他');
                            })->saving(function ($paths) {
                                return implode(',', $paths);
                            });
                        });

                    $form->radio('treatment', '既往治疗')->options([1 => '是', 2 => '否'])
                        ->when(1, function (Form\BlockForm $form) {
                            $form->datetime('treatment_date', '治疗时间');
                            $form->textarea('treatment_specific', '治疗方案');
                            $form->text('treatment_effect', '治疗效果');
                            $form->radio('treatment_return_visit', '是否随访')->options([1 => '是', 2 => '否']);
                        })->when(2, function (Form\BlockForm $form) {
                            $form->textarea('treatment_untreated', '未治疗原因');
                        });
                });
            });
            $form->block(12, function (Form\BlockForm $form) {

                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;临床检查');
                $form->checkbox('digestive_diseases', '消化疾病相关检查')
                    ->options(Data::getPastCheckSuccess())
                    ->when(2, function (Form\BlockForm $form) {
                        $form->text('digestive_diseases_laboratory', '实验室检查结果');
                    })->when(3, function (Form\BlockForm $form) {
                        $form->text('digestive_diseases_image', '影像学检查结果');
                    })->when(4, function (Form\BlockForm $form) {
                        $form->text('digestive_diseases_pathology', '病理检查结果');
                    })->when(5, function (Form\BlockForm $form) {
                        $form->text('digestive_diseases_other', '其他检查结果');
                    })->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->radio('helicobacter_pylori', '幽门螺旋杆菌感染')->options([1 => '阳性', 2 => '阴性'])->required();

                $form->checkbox('comorbidities', '合并症相关检查')
                    ->options(Data::getComorbidities())
                    ->when(1, function (Form\BlockForm $form) {
                        $form->text('comorbidities_anxiety', '焦虑(汉密尔顿焦虑量表评分)');
                    })->when(2, function (Form\BlockForm $form) {
                        $form->text('comorbidities_depression', '抑郁(汉密尔顿焦虑量表评分)');
                    })->when(3, function (Form\BlockForm $form) {
                        $form->text('comorbidities_other', '其他');
                    })->saving(function ($paths) {
                        return implode(',', $paths);
                    });
            });
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;疾病诊断');
                $form->radio('disease_identify', '是否进行鉴别诊断')->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form\BlockForm $form) {
                        $form->text('disease_identify_true', '与那些疾病进行鉴别诊断');
                    })->when(2, function (Form\BlockForm $form) {
                        $form->text('disease_identify_false', '原因');
                    })->required();
                $form->checkbox('disease_diagnosis', '诊断')
                    ->options(Data::getDiagnosis())
                    ->when(1, function (Form\BlockForm $form) {
                        $form->divider('慢性胃炎疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '慢性胃炎疗效评估(至少填写2项)', function ($form) {
                        $form->text('gastritis_clinical', '临床症状(症状积分)');
                        $form->text('gastritis_laboratory', '实验室指标(如炎性因子、肠胃激素等)、Hp根除率等');
                        $form->text('gastritis_curativeeffect', '疗效评价');
                        $form->text('gastritis_complications', '并发症等');
                        // });
                    })->when(2, function (Form\BlockForm $form) {
                        $form->divider('消化性溃疡疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '消化性溃疡疗效评估(至少填写2项)', function ($form) {
                        $form->text('ulcer_clinical', '临床症状(症状积分)');
                        $form->text('ulcer_evaluation', '疗效评价(溃疡愈合情况)');
                        $form->text('ulcer_hp', 'Hp根除率');
                        $form->text('ulcer_complications', '并发症情况');
                        // });
                    })->when(3, function (Form\BlockForm $form) {
                        $form->divider('功能性消化不良疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '功能性消化不良疗效评估(至少填写2项)', function ($form) {
                        $form->text('dyspepsia_evaluation', '症状评价(症状积分)');
                        $form->text('dyspepsia_evaluate', '胃功能评估(GE或LNLT检查)');
                        $form->text('dyspepsia_quality', '生活质量评估(FDDQL或SF-NDI量表)');
                        $form->text('ulcer_other', '其他疗效评估');
                        // });
                    })->when(4, function (Form\BlockForm $form) {
                        $form->divider('其他疾病疗效评估(至少填写2项)');
                        $form->text('disease_diagnosis_input', '其他');
                        // $form->embeds('column_name', '其他疾病疗效评估(至少填写2项)', function ($form) {
                        $form->text('otherdiseases_clinical', '临床症状');
                        $form->text('otherdiseases_other', '其他指标评估');
                        // });
                    })->required()
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });


                $form->checkbox('tcm_syndrome_type', '中医证型')
                    ->options(Data::getTcmSyndromeType())
                    ->when([1, 2], function (Form\BlockForm $form) {
                        $form->divider('治疗前症状程度评分  (0:无症状 1:轻度  2:中度  3:严重  4:极严重)');
                        $form->slider('pre_degree_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1, 'values' => ['无', '轻度', '中度', '重度', '极严重'], 'grid' => true]);
                        $form->slider('pre_degree_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);


                        $form->divider('治疗前症状频率评分  (0:无 1:1d/周  2:2~3d/周  3:4~5d/周  4:持续)');
                        $form->slider('pre_frequency_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);

                        $form->divider('治疗后症状程度评分  (0:无症状 1:轻度  2:中度  3:严重  4:极严重)');
                        $form->slider('post_degree_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);

                        $form->divider('治疗后症状频率评分  (0:无 1:1d/周  2:2~3d/周  3:4~5d/周  4:持续)');
                        $form->slider('post_frequency_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                    })
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->divider();
                $form->text('disease_classification', '具体分类、分级/分期');
                $form->radio('disease_comorbidities', '合并病症')->options(Data::getComorbiditie())
                    ->when(3, function (Form\BlockForm $form) {
                        $form->text('disease_comorbidities_input', '其他');
                    });
            });
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;治疗方案');
                $form->radio('treatment_plan', '用药方案')
                    ->options([1 => '胃苏颗粒'])
                    ->required();
                // $form->datetimeRange('1', '2', '用药起始时间');
                $form->date('treatment_plan_date', '用药起始时间');
                $form->text('treatment_mg', '治疗剂量')->help('i.v.gtt,____mg');
                $form->text('treatment_bag', '治疗剂量')->help('____袋');
                $form->radio('treatment_situation', '合并用药情况')
                    ->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form\BlockForm $form) {
                        $form->text('treatment_situation_input', '具体药物及计量');
                    })
                    ->required();
                $form->radio('treatment_dose', '治疗期间是否调整治疗药物计量')
                    ->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form\BlockForm $form) {
                        $form->text('treatment_dose_input', '原因');
                    })
                    ->required();
            });
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;不良反应监测');
                $form->radio('adversereaction', '患者是否产生不良反应')
                    ->options([1 => '无', 2 => '有'])
                    ->required();
                $form->checkbox('adversereaction_group', '不良反应表现')
                    ->options(Data::getAdverseReactions())
                    ->when(16, function (Form\BlockForm $form) {
                        $form->text('adversereaction_group_input', '其他不良反应');
                    })
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->datetime('adversereaction_date', '反应发生日期');
                $form->radio('adversereaction_immediately', '是否用药后立即发生')
                    ->options([1 => '是', 2 => '否']);
                $form->text('adversereaction_time_input', '反应发生时间,用药后几小时');
                $form->radio('adversereaction_degree', '不良反应的严重情况')
                    ->options(Data::getAdverseReactionsSeverity());
                $form->radio('adversereaction_related', '是否与观察药物相关')
                    ->options(Data::getIsRelated());
                $form->text('adversereaction_strategy', '不良反应的应对策略');
            });
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('<span style="color:red">*</span>&nbsp;审核');
                if (Admin::user()->can('medical-audit') || Admin::user()->isAdministrator()) {
                    $form->showFooter();
                    $form->radio('status', '状态')->when([1, 2], function (Form\BlockForm $form) {
                        $form->textarea('review_comments', '审核意见');

                        $form->hidden('examine_time');
                        // $form->saving(function ($form) {

                        //     $form->examine_time = date('Y-m-d H:i:s', time());
                        // });
                    })->options([0 => '未审核', 1 => '已审核', 2 => '驳回'])->default(0)->saving(function ($form) {
                        $form->examine_time = date('Y-m-d H:i:s', Carbon::now());
                    });
                }
            });












            // $form->saving(function (Form $form) {
            //     dd($form->input());
            // });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form_bak()
    {
        return Form::make(new Medical(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableViewButton();
            // $form->disableSubmitButton();
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $form->disableDeleteButton();
            }

            $form->hidden('status')
                // ->customFormat(function ($v) {
                //     return $v == 1 ? 1 : 0;
                // })
                ->saving(function ($v) {
                    return $v;
                });
            $form->tab('患者基本信息', function (Form $form) {
                $form->column(6, function (Form $form) {
                    $form->text('name', '患者姓名')->required();
                    $form->number('age', '年龄')->placeholder('请输入年龄')->required();

                    $form->radio('sex', '性别')->options([1 => '男', 2 => '女'])->required();
                    $form->text('height', '身高')->required()->help('CM');
                    $form->text('weight', '体重')->required()->help('KG');

                    $form->radio('source', '病例来源')->when(4, function (Form $form) {
                        $form->text('source_input', '其他来源')->placeholder('其他病例来源');
                    })->options(Data::getMedicalSource());
                    $form->text('bim', 'BIM')->placeholder('请输入内容')->required();
                    $form->radio('bim_radio', '判断指标')->options(Data::getBimType())->required();

                    $form->checkbox('symptom', '主诉症状')->options(Data::getSymptom())->when(11, function (Form $form) {
                        $form->text('symptom_input', '其他');
                    })->required()->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                    $form->radio('disease_code', '疾病状态')->options([1 => '新发', 2 => '已确诊疾病'])->required();
                    $form->text('course_disease', '病程')->required();

                    $form->radio('drink_alcohol', '饮酒史')->options([1 => '是', 2 => '否'])->when(1, function (Form $form) {
                        $form->text('drink_alcohol_quantity', '饮酒量')->placeholder('每日（）ml');
                        $form->text('drink_alcohol_frequency', '饮酒频率')->placeholder('每周（）次');
                    });

                    $form->radio('smoking_history', '吸烟史')->options([1 => '是', 2 => '否'])->when(1, function (Form $form) {
                        $form->text('smoking_history_year', '吸烟年限')->placeholder('吸烟（）年');
                        $form->text('smoking_history_quantity', '吸烟量')->placeholder('每日（）根');
                    });
                });

                $form->column(6, function (Form $form) {


                    $form->radio('family_historyL', '家族史')->options([1 => '是', 2 => '否']);
                    $form->radio('past_historyL', '既往史')->options([1 => '是', 2 => '否']);

                    $form->text('medical_history', '其他既往史');

                    $form->radio('allergy', '过敏史')->options([1 => '无', 2 => '有'])->when(2, function (Form $form) {
                        $form->text('allergy_input', '其他过敏史')->placeholder('请输入其他过敏史');
                    });

                    $form->radio('check', '既往检查')->options([1 => '是', 2 => '否'])
                        ->when(1, function (Form $form) {
                            $form->checkbox('check_true', '检查项目')
                                ->options(Data::getPastCheckSuccess())
                                ->when(2, function (Form $form) {
                                    $form->text('check_true_laboratory', '实验室检查结果');
                                })->when(3, function (Form $form) {
                                    $form->text('check_true_image', '影像学检查结果');
                                })->when(4, function (Form $form) {
                                    $form->text('check_true_pathology', '病理检查结果');
                                })->when(5, function (Form $form) {
                                    $form->text('check_true_other', '其他检查结果');
                                })->saving(function ($paths) {
                                    return implode(',', $paths);
                                });
                        })->when(2, function (Form $form) {
                            $form->checkbox('check_false', '未进行检查原因')->options(Data::getPastCheckFail())->when(6, function (Form $form) {
                                $form->text('check_false_input', '其他');
                            })->saving(function ($paths) {
                                return implode(',', $paths);
                            });
                        });

                    $form->radio('treatment', '既往治疗')->options([1 => '是', 2 => '否'])
                        ->when(1, function (Form $form) {
                            $form->datetime('treatment_date', '治疗时间');
                            $form->textarea('treatment_specific', '治疗方案');
                            $form->text('treatment_effect', '治疗效果');
                            $form->radio('treatment_return_visit', '是否随访')->options([1 => '是', 2 => '否']);
                        })->when(2, function (Form $form) {
                            $form->textarea('treatment_untreated', '未治疗原因');
                        });
                });
            })->tab('临床检查', function (Form $form) {
                $form->checkbox('digestive_diseases', '消化疾病相关检查')
                    ->options(Data::getPastCheckSuccess())
                    ->when(2, function (Form $form) {
                        $form->text('digestive_diseases_laboratory', '实验室检查结果');
                    })->when(3, function (Form $form) {
                        $form->text('digestive_diseases_image', '影像学检查结果');
                    })->when(4, function (Form $form) {
                        $form->text('digestive_diseases_pathology', '病理检查结果');
                    })->when(5, function (Form $form) {
                        $form->text('digestive_diseases_other', '其他检查结果');
                    })->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->radio('helicobacter_pylori', '幽门螺旋杆菌感染')->options([1 => '阳性', 2 => '阴性'])->required();

                $form->checkbox('comorbidities', '合并症相关检查')
                    ->options(Data::getComorbidities())
                    ->when(1, function (Form $form) {
                        $form->text('comorbidities_anxiety', '焦虑(汉密尔顿焦虑量表评分)');
                    })->when(2, function (Form $form) {
                        $form->text('comorbidities_depression', '抑郁(汉密尔顿焦虑量表评分)');
                    })->when(3, function (Form $form) {
                        $form->text('comorbidities_other', '其他');
                    })->saving(function ($paths) {
                        return implode(',', $paths);
                    });
            })->tab('疾病诊断', function (Form $form) {
                $form->radio('disease_identify', '是否进行鉴别诊断')->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form $form) {
                        $form->text('disease_identify_true', '与那些疾病进行鉴别诊断');
                    })->when(2, function (Form $form) {
                        $form->text('disease_identify_false', '原因');
                    })->required();
                $form->checkbox('disease_diagnosis', '诊断')
                    ->options(Data::getDiagnosis())
                    ->when(1, function (Form $form) {
                        $form->divider('慢性胃炎疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '慢性胃炎疗效评估(至少填写2项)', function ($form) {
                        $form->text('gastritis_clinical', '临床症状(症状积分)');
                        $form->text('gastritis_laboratory', '实验室指标(如炎性因子、肠胃激素等)、Hp根除率等');
                        $form->text('gastritis_curativeeffect', '疗效评价');
                        $form->text('gastritis_complications', '并发症等');
                        // });
                    })->when(2, function (Form $form) {
                        $form->divider('消化性溃疡疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '消化性溃疡疗效评估(至少填写2项)', function ($form) {
                        $form->text('ulcer_clinical', '临床症状(症状积分)');
                        $form->text('ulcer_evaluation', '疗效评价(溃疡愈合情况)');
                        $form->text('ulcer_hp', 'Hp根除率');
                        $form->text('ulcer_complications', '并发症情况');
                        // });
                    })->when(3, function (Form $form) {
                        $form->divider('功能性消化不良疗效评估(至少填写2项)');
                        // $form->embeds('column_name', '功能性消化不良疗效评估(至少填写2项)', function ($form) {
                        $form->text('dyspepsia_evaluation', '症状评价(症状积分)');
                        $form->text('dyspepsia_evaluate', '胃功能评估(GE或LNLT检查)');
                        $form->text('dyspepsia_quality', '生活质量评估(FDDQL或SF-NDI量表)');
                        $form->text('ulcer_other', '其他疗效评估');
                        // });
                    })->when(4, function (Form $form) {
                        $form->divider('其他疾病疗效评估(至少填写2项)');
                        $form->text('disease_diagnosis_input', '其他');
                        // $form->embeds('column_name', '其他疾病疗效评估(至少填写2项)', function ($form) {
                        $form->text('otherdiseases_clinical', '临床症状');
                        $form->text('otherdiseases_other', '其他指标评估');
                        // });
                    })->required()
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });


                $form->checkbox('tcm_syndrome_type', '中医证型')
                    ->options(Data::getTcmSyndromeType())
                    ->when([1, 2], function (Form $form) {
                        $form->divider('治疗前症状程度评分  (0:无症状 1:轻度  2:中度  3:严重  4:极严重)');
                        $form->slider('pre_degree_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1, 'values' => ['无', '轻度', '中度', '重度', '极严重'], 'grid' => true]);
                        $form->slider('pre_degree_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_degree_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);


                        $form->divider('治疗前症状频率评分  (0:无 1:1d/周  2:2~3d/周  3:4~5d/周  4:持续)');
                        $form->slider('pre_frequency_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('pre_frequency_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);

                        $form->divider('治疗后症状程度评分  (0:无症状 1:轻度  2:中度  3:严重  4:极严重)');
                        $form->slider('post_degree_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_degree_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);

                        $form->divider('治疗后症状频率评分  (0:无 1:1d/周  2:2~3d/周  3:4~5d/周  4:持续)');
                        $form->slider('post_frequency_pain_chest_ribs', '腹肋胀痛')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_epigastric_full', '脘腹痞满')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_be_upset', '心烦')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_eructation_frequent', '嗳气频做')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                        $form->slider('post_frequency_mild_breathing', '善太息(频繁叹气)')->options(['max' => 4, 'min' => 0, 'step' => 1]);
                    })
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->divider();
                $form->text('disease_classification', '具体分类、分级/分期');
                $form->radio('disease_comorbidities', '合并病症')->options(Data::getComorbiditie())
                    ->when(3, function (Form $form) {
                        $form->text('disease_comorbidities_input', '其他');
                    });
            })->tab('治疗方案', function (Form $form) {

                $form->radio('treatment_plan', '用药方案')
                    ->options([1 => '胃苏颗粒'])
                    ->required();
                // $form->datetimeRange('1', '2', '用药起始时间');
                $form->date('treatment_plan_date', '用药起始时间');
                $form->text('treatment_mg', '治疗剂量')->help('i.v.gtt,____mg');
                $form->text('treatment_bag', '治疗剂量')->help('____袋');
                $form->radio('treatment_situation', '合并用药情况')
                    ->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form $form) {
                        $form->text('treatment_situation_input', '具体药物及计量');
                    })
                    ->required();
                $form->radio('treatment_dose', '治疗期间是否调整治疗药物计量')
                    ->options([1 => '是', 2 => '否'])
                    ->when(1, function (Form $form) {
                        $form->text('treatment_dose_input', '原因');
                    })
                    ->required();
            })->tab('不良反应监测', function (Form $form) {
                $form->radio('adversereaction', '患者是否产生不良反应')
                    ->options([1 => '无', 2 => '有'])
                    ->required();
                $form->checkbox('adversereaction_group', '不良反应表现')
                    ->options(Data::getAdverseReactions())
                    ->when(16, function (Form $form) {
                        $form->text('adversereaction_group_input', '其他不良反应');
                    })
                    ->saving(function ($paths) {
                        return implode(',', $paths);
                    });
                $form->datetime('adversereaction_date', '反应发生日期');
                $form->radio('adversereaction_immediately', '是否用药后立即发生')
                    ->options([1 => '是', 2 => '否']);
                $form->text('adversereaction_time_input', '反应发生时间,用药后几小时');
                $form->radio('adversereaction_degree', '不良反应的严重情况')
                    ->options(Data::getAdverseReactionsSeverity());
                $form->radio('adversereaction_related', '是否与观察药物相关')
                    ->options(Data::getIsRelated());
                $form->text('adversereaction_strategy', '不良反应的应对策略');
            })->tab('审核', function (Form $form) {
                if (Admin::user()->can('medical-audit') || Admin::user()->isAdministrator()) {
                    $form->radio('status', '状态')->when([1, 2], function (Form $form) {
                        $form->textarea('review_comments', '审核意见');

                        $form->hidden('examine_time');
                        $form->saving(function (Form $form) {
                            $form->examine_time = date('Y-m-d H:i:s', time());
                        });
                    })->options([0 => '未审核', 1 => '已审核', 2 => '驳回'])->default(0);
                }
            });












            // $form->saving(function (Form $form) {
            //     dd($form->input());
            // });
        });
    }
}
